//
//  CZEmotionPageView.swift
//  CZWeibo
//
//  Created by 左凯旭 on 16/8/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit


private let CZEmotionPageViewCellID = "CZEmotionPageViewCell"

class CZEmotionPageView: UICollectionView {

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: - 设置UI
    func setupUI(){
        //设置代理
        dataSource = self
        
        //注册cell
        registerClass(CZEmotionPageViewCell.self, forCellWithReuseIdentifier: CZEmotionPageViewCellID)
  
    }
    //设置Layout
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //设置layout
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        //设置frame
        layout.itemSize = self.frame.size
        //设置滚动的方向
        layout.scrollDirection = .Horizontal
        //设置间距
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
    }
    
}


//MARK: - 数据源和代理方法


extension CZEmotionPageView: UICollectionViewDataSource {
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return CZEmoticonTools.shareTools.allEmoticons.count
    }
    
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return CZEmoticonTools.shareTools.allEmoticons[section].count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
      let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CZEmotionPageViewCellID, forIndexPath: indexPath) as! CZEmotionPageViewCell
        
//        cell.indexpath = indexPath
        
        cell.emoticons = CZEmoticonTools.shareTools.allEmoticons[indexPath.section][indexPath.item]
        
        cell.backgroundColor = self.backgroundColor
        
        return cell
    }
    
}








